package com.masterclient.app.modules.goods;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.eoner.baselibrary.bean.goods.CouponMessage;
import com.eoner.baselibrary.bean.goods.ProductPromotionMessage;
import com.eoner.baselibrary.bean.goods.ProductPromotionProperty;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.App;
import com.masterclient.app.R;
import com.masterclient.app.base.FragmentHostActivity;
import com.masterclient.app.utils.CommonUtil;
import java.util.List;


public class CommodityCouponDialog extends Dialog {

    private Context mContext;
    private String title;
    private CommodityCouponAdapter couponAdapter;
    List<CouponMessage> list;
    List<ProductPromotionMessage> promotionList;
    private String desc;
    private CommodityDetailData.MeetReducePromotion activityDes;

    public CommodityCouponDialog(@NonNull Context context, String title, List<CouponMessage> list
            , List<ProductPromotionMessage> promotionList, String desc, CommodityDetailData.MeetReducePromotion activityDes) {
        super(context, R.style.MyDialog);
        this.list = list;
        this.promotionList = promotionList;
        this.desc = desc;
        mContext = context;
        this.title = title;
        this.activityDes = activityDes;
    }

    public void notifyDataChange(List<CouponMessage> list, List<ProductPromotionMessage> promotionList
            , String desc) {
        this.list = list;
        this.promotionList = promotionList;
        this.desc = desc;
        couponAdapter.setNewData(list);
        couponAdapter.notifyDataSetChanged();
    }

    private TextView tvTitle;
    private TextView tvCancel;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_coupon);
        setCanceledOnTouchOutside(true);
        tvTitle = findViewById(R.id.tv_title);
        tvCancel = findViewById(R.id.tv_cancel);
        recyclerView = findViewById(R.id.recyclerView);
        tvTitle.setText(title);

        couponAdapter = new CommodityCouponAdapter(mContext);
        couponAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tv_btn) {

                onButtonClickListener.btnClick(list.get(position).getCoupon_code(), position);
            }
        });
        View headView = View.inflate(mContext, R.layout.item_coupon_head, null);
        //满减、赠品
        LinearLayout viewPresent = headView.findViewById(R.id.view_present);
        LinearLayout ll_present = headView.findViewById(R.id.ll_present);
        LinearLayout ll_shop_present_container = headView.findViewById(R.id.ll_coupon_present_container);
        View flActivity = headView.findViewById(R.id.fl_activity);
        TextView tvActivityTag = headView.findViewById(R.id.tv_activity_tag);
        TextView tvActivityTime = headView.findViewById(R.id.tv_activity_time);
        TextView tvActivityDes = headView.findViewById(R.id.tv_activity_des);
        TextView tvDesc = headView.findViewById(R.id.tv_desc);
        TextView tvCouponTitle = headView.findViewById(R.id.tv_coupon_title);

        //满减
        if (activityDes != null && !TextUtils.isEmpty(activityDes.getActivity_id())
                && !TextUtils.isEmpty(activityDes.getLabel_name())
                && !TextUtils.isEmpty(activityDes.getEndAt())
                && !TextUtils.isEmpty(activityDes.getName())) {
            flActivity.setVisibility(View.VISIBLE);
            flActivity.setOnClickListener(view -> {
                onButtonClickListener.meetReduceCallback();
                Fragment fragment = (Fragment) ARouter.getInstance().build(AroutePath.GATHER_GOODS_FRAGMENT)
                        .withString("activityId", activityDes.getActivity_id())
                        .navigation();
                FragmentHostActivity.openFragment(App.getContext(), fragment);
            });
            tvActivityTag.setText(activityDes.getLabel_name());
            tvActivityTime.setText(activityDes.getEndAt() + "结束");
            tvActivityDes.setText(activityDes.getName());
        } else {
            flActivity.setVisibility(View.GONE);
        }

        tvDesc.setText("赠品  " + desc);
        if (promotionList != null && promotionList.size() > 0) {
            ll_present.setVisibility(View.VISIBLE);
            ll_shop_present_container.setVisibility(View.VISIBLE);
            ll_shop_present_container.removeAllViews();
            for (ProductPromotionMessage promotionBean : promotionList) {
                View view_present_goods = View.inflate(mContext, R.layout.item_present, null);
                ImageView ivPresentGood = view_present_goods.findViewById(R.id.iv_good);
                TextView tvTag = view_present_goods.findViewById(R.id.tv_tag);
                TextView tvPresentTitle = view_present_goods.findViewById(R.id.tv_title);
                TextView tvPresentSkus = view_present_goods.findViewById(R.id.tv_skus);
                TextView tvPresentPrice = view_present_goods.findViewById(R.id.tv_price);
                TextView tvPresentNum = view_present_goods.findViewById(R.id.tv_num);
                view_present_goods.setOnClickListener(view1 -> {
                    if (promotionBean.getType() != null) {
                        onButtonClickListener.giveawayCallback(promotionBean.getActivity_id(),promotionBean.getActivity_name());
                        CommonUtil.goAnyWhere(mContext,  promotionBean.getType(), promotionBean.getTarget_id(), "", "", "");
                    }
                });

                GlideUtil.displayRoundImage(mContext, promotionBean.getImage(), ivPresentGood, 7, R.mipmap.defaults);
                tvTag.setVisibility(View.GONE);
                tvPresentTitle.setText(promotionBean.getName());
                String skuStr = "";
                for (ProductPromotionProperty sku : promotionBean.getProperties()) {
                    skuStr = skuStr + sku.getAlias_name();
                }
                tvPresentSkus.setText(skuStr);
                tvPresentPrice.setVisibility(View.GONE);
                tvPresentNum.setText("x" + promotionBean.getNum());
                ll_shop_present_container.addView(view_present_goods);
            }
        } else {
            ll_present.setVisibility(View.GONE);
            ll_shop_present_container.setVisibility(View.GONE);
        }

        if (activityDes == null && (promotionList == null || promotionList.size() <= 0)) {
            viewPresent.setVisibility(View.GONE);
        } else {
            viewPresent.setVisibility(View.VISIBLE);
        }

        if (list != null && list.size() > 0){
            tvCouponTitle.setVisibility(View.VISIBLE);
        }else {
            tvCouponTitle.setVisibility(View.GONE);
        }

        couponAdapter.addHeaderView(headView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(couponAdapter);
        couponAdapter.setNewData(list);

        tvCancel.setOnClickListener(view -> dismiss());
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        window.setWindowAnimations(R.style.bottomShow);
        WindowManager.LayoutParams windowparams = window.getAttributes();
        window.setGravity(Gravity.BOTTOM);
        windowparams.height = (int) (ScreenUtils.getScreenHeight() * 0.58);
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }


    public interface OnButtonClickListener {
        void btnClick(String code, int position);
        void meetReduceCallback();
        void giveawayCallback(String activity_id, String activity_name);
    }

    private OnButtonClickListener onButtonClickListener;

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }
}
